package net.cyue.android.js.module;

import android.app.Activity;
import android.content.ContentResolver;
import android.net.Uri;

import net.cyue.android.Util;
import com.kongzue.dialogx.dialogs.PopTip;
import com.wragony.android.jsbridge.module.JSBridgeMethod;
import com.wragony.android.jsbridge.module.JsModule;
import com.wragony.android.jsbridge.module.datatype.JBCallback;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

public class UtilJSModule extends JsModule
{
    private final Activity activity;

    @Override
    public String getModuleName() {
        return "Util";
    }

    public UtilJSModule(Activity activity)
    {
        this.activity = activity;
    }


    @JSBridgeMethod
    public String getUniqueID()
    {
        return Util.getUniqueID();
    }


    @JSBridgeMethod
    public void copyFileOrDirectory(String filePath, String savePath, JBCallback finishedCallback)
    {
        try {
            Util.copyFileOrDirectory(filePath, savePath);
            if (finishedCallback != null)
                finishedCallback.apply();
        } catch (IOException e) {
            this.activity.runOnUiThread(() -> {
                PopTip.show(e.getMessage());
            });
        }
    }

    @JSBridgeMethod
    public void moveFileOrDirectory(String filePath, String savePath, JBCallback finishedCallback)
    {
        try {
            Util.moveFileOrDirectory(filePath, savePath);
            if (finishedCallback != null)
                finishedCallback.apply();
        } catch (IOException e) {
            this.activity.runOnUiThread(() -> {
                PopTip.show(e.getMessage());
            });
        }
    }


    @JSBridgeMethod
    public void deleteFileOrDirectory(String path, JBCallback successCallback, JBCallback failedCallback)
    {
        if (Util.deleteFileOrDirectory(new File(path))) {
            if (successCallback != null)
                successCallback.apply();
        } else {
            if (failedCallback != null)
                failedCallback.apply();
        }
    }


    /**
     * 会给 js 一个 pictureUri
     */
    @JSBridgeMethod
    public void openAlbum(JBCallback callback)
    {
        Util.openAlbum(this.activity, pictureUri -> {
            if (callback != null)
                callback.apply(pictureUri);
        });
    }


    @JSBridgeMethod
    public void toBrowser(String url)
    {
        Util.toBrowser(this.activity, url);
    }


    /**
     * 返回 base64 数据
     */
    @JSBridgeMethod
    public String readFileByPathToBase64(String path)
    {
        try {
            return Util.readFileToBaseString(path);
        } catch (IOException e) {
            this.activity.runOnUiThread(() -> {
//                PopTip.show(e.getMessage());
            });
        }
        return null;
    }




    @JSBridgeMethod
    public String readFileByUriToBase64(String uriString)
    {
        if (uriString == null || uriString.isEmpty())
            return null;

        Uri uri = Uri.parse(uriString);
        ContentResolver resolver = this.activity.getContentResolver();
        if (resolver == null)
            return null;

        try {
            InputStream input = resolver.openInputStream(uri);
            if (input == null)
                return null;
            return Util.readFileToBaseString(input);
        } catch (IOException e) {
            this.activity.runOnUiThread(() -> {
//                PopTip.show(e.getMessage());
            });
        }
        return null;
    }


    @JSBridgeMethod
    public void downloadFile(String url, String name, JBCallback finishedCallback)
    {
        Util.downloadFile(
            this.activity,
            url,
            name,
            (file) -> {
                if (finishedCallback != null)
                    finishedCallback.apply(file.getAbsolutePath());
            }
        );
    }


}


